package com.otitan.la.forest.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.otitan.la.forest.system.dto.DicTypeDTO;
import com.otitan.la.forest.system.entity.DicTypeEntity;
import com.otitan.la.forest.system.mapper.DicTypeMapper;
import com.otitan.la.forest.system.service.DicTypeService;
import com.otitan.webapp.framework.basepro.exception.code.BaseResponseCode;
import com.otitan.webapp.framework.basepro.model.DataResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

import java.util.List;

@Slf4j
@Service
public class DicTypeServiceImpl extends ServiceImpl<DicTypeMapper, DicTypeEntity> implements DicTypeService {

    @Override
    public DataResult add(DicTypeEntity dicType, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return DataResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        // 字典类型不能重复
        LambdaQueryWrapper<DicTypeEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(DicTypeEntity::getDicTypeCode, dicType.getDicTypeCode());
        int count = this.count(queryWrapper);
        if (count > 0) {
            return DataResult.fail("字典类型已存在");
        }
        boolean isSuccess = this.save(dicType);
        return isSuccess ? DataResult.success() : DataResult.fail(BaseResponseCode.DATA_NOT_EXIST.getMsg());
    }

    @Override
    public DataResult batchDelete(List<String> ids) {
        boolean isSuccess = this.removeByIds(ids);
        log.debug("\n>>>>>>>>> 【删除字典类型表】 <<<<<<<<<\n操作结果：{}，ids：{}\n>>>>>>>>> ---- <<<<<<<<<", isSuccess, ids);
        return DataResult.success();
    }

    @Override
    public DataResult editor(DicTypeEntity dicType, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return DataResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        // 字典类型不能重复
        LambdaQueryWrapper<DicTypeEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(DicTypeEntity::getDicTypeCode, dicType.getDicTypeCode());
        queryWrapper.ne(DicTypeEntity::getId, dicType.getId());
        int count = this.count(queryWrapper);
        if (count > 0) {
            return DataResult.fail("字典类型已存在");
        }

        boolean isSuccess = this.updateById(dicType);
        return isSuccess ? DataResult.success() : DataResult.fail(BaseResponseCode.DATA_NOT_EXIST.getMsg());
    }

    @Override
    public DataResult findPageData(long page, long size, DicTypeDTO dicType) {
        Page pagePlug = new Page(page, size);
        LambdaQueryWrapper<DicTypeEntity> queryWrapper = Wrappers.lambdaQuery();
        if (StrUtil.isNotEmpty(dicType.getDicTypeName())) {
            queryWrapper.like(DicTypeEntity::getDicTypeName, dicType.getDicTypeName());
        }
        queryWrapper.orderByAsc(DicTypeEntity::getOrderNo);
        IPage<DicTypeEntity> iPage = this.page(pagePlug, queryWrapper);
        return DataResult.success(iPage);
    }

    @Override
    public DataResult findOne(String id) {
        return DataResult.success(this.getById(id));
    }

    @Override
    public DataResult getDicTypeOptions() {
        LambdaQueryWrapper<DicTypeEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.orderByDesc(DicTypeEntity::getOrderNo);
        return DataResult.success(this.list(queryWrapper));
    }

    @Override
    public boolean existDicType(String dicTypeCode) {

        LambdaQueryWrapper<DicTypeEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(DicTypeEntity::getDicTypeCode, dicTypeCode);
        int count = this.count(queryWrapper);
        return count > 0 ? true : false;
    }
}